﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using PS.DBUtility;
namespace PS.DAL
{
    /// <summary>
    /// 数据访问类:ExamResult
    /// </summary>
    public partial class ExamResult
    {
        public ExamResult()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录

        /// </summary>
        public bool Exists(int ExamResultID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ExamResult");
            strSql.Append(" where ExamResultID=@ExamResultID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExamResultID", SqlDbType.Int,4)
};
            parameters[0].Value = ExamResultID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据

        /// </summary>
        public int Add(PS.Model.ExamResult model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ExamResult(");
            strSql.Append("ExamRecordID,ExamQuestionsID,Answer,ExamScore,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4,SpareDecimal5,SpareDecimal6,SpareDecimal7,SpareDecimal8)");
            strSql.Append(" values (");
            strSql.Append("@ExamRecordID,@ExamQuestionsID,@Answer,@ExamScore,@Creator,@CreateTime,@Editor,@EditTime,@DispOrder,@ValidMark,@Spare1,@Spare2,@Spare3,@Spare4,@SpareDecimal5,@SpareDecimal6,@SpareDecimal7,@SpareDecimal8)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ExamRecordID", SqlDbType.Int,4),
					new SqlParameter("@ExamQuestionsID", SqlDbType.Int,4),
					new SqlParameter("@Answer", SqlDbType.NVarChar,4000),
					new SqlParameter("@ExamScore", SqlDbType.Decimal,9),
					new SqlParameter("@Creator", SqlDbType.VarChar,20),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Editor", SqlDbType.VarChar,20),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@DispOrder", SqlDbType.Int,4),
					new SqlParameter("@ValidMark", SqlDbType.Int,4),
					new SqlParameter("@Spare1", SqlDbType.VarChar,200),
					new SqlParameter("@Spare2", SqlDbType.VarChar,200),
					new SqlParameter("@Spare3", SqlDbType.VarChar,200),
					new SqlParameter("@Spare4", SqlDbType.VarChar,200),
					new SqlParameter("@SpareDecimal5", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal6", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal7", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal8", SqlDbType.Decimal,9)};
            parameters[0].Value = model.ExamRecordID;
            parameters[1].Value = model.ExamQuestionsID;
            parameters[2].Value = model.Answer;
            parameters[3].Value = model.ExamScore;
            parameters[4].Value = model.Creator;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.Editor;
            parameters[7].Value = model.EditTime;
            parameters[8].Value = model.DispOrder;
            parameters[9].Value = model.ValidMark;
            parameters[10].Value = model.Spare1;
            parameters[11].Value = model.Spare2;
            parameters[12].Value = model.Spare3;
            parameters[13].Value = model.Spare4;
            parameters[14].Value = model.SpareDecimal5;
            parameters[15].Value = model.SpareDecimal6;
            parameters[16].Value = model.SpareDecimal7;
            parameters[17].Value = model.SpareDecimal8;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据

        /// </summary>
        public bool Update(PS.Model.ExamResult model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ExamResult set ");
            strSql.Append("ExamRecordID=@ExamRecordID,");
            strSql.Append("ExamQuestionsID=@ExamQuestionsID,");
            strSql.Append("Answer=@Answer,");
            strSql.Append("ExamScore=@ExamScore,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Editor=@Editor,");
            strSql.Append("EditTime=@EditTime,");
            strSql.Append("DispOrder=@DispOrder,");
            strSql.Append("ValidMark=@ValidMark,");
            strSql.Append("Spare1=@Spare1,");
            strSql.Append("Spare2=@Spare2,");
            strSql.Append("Spare3=@Spare3,");
            strSql.Append("Spare4=@Spare4,");
            strSql.Append("SpareDecimal5=@SpareDecimal5,");
            strSql.Append("SpareDecimal6=@SpareDecimal6,");
            strSql.Append("SpareDecimal7=@SpareDecimal7,");
            strSql.Append("SpareDecimal8=@SpareDecimal8");
            strSql.Append(" where ExamResultID=@ExamResultID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExamRecordID", SqlDbType.Int,4),
					new SqlParameter("@ExamQuestionsID", SqlDbType.Int,4),
					new SqlParameter("@Answer", SqlDbType.NVarChar,4000),
					new SqlParameter("@ExamScore", SqlDbType.Decimal,9),
					new SqlParameter("@Creator", SqlDbType.VarChar,20),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Editor", SqlDbType.VarChar,20),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@DispOrder", SqlDbType.Int,4),
					new SqlParameter("@ValidMark", SqlDbType.Int,4),
					new SqlParameter("@Spare1", SqlDbType.VarChar,200),
					new SqlParameter("@Spare2", SqlDbType.VarChar,200),
					new SqlParameter("@Spare3", SqlDbType.VarChar,200),
					new SqlParameter("@Spare4", SqlDbType.VarChar,200),
					new SqlParameter("@SpareDecimal5", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal6", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal7", SqlDbType.Decimal,9),
					new SqlParameter("@SpareDecimal8", SqlDbType.Decimal,9),
					new SqlParameter("@ExamResultID", SqlDbType.Int,4)};
            parameters[0].Value = model.ExamRecordID;
            parameters[1].Value = model.ExamQuestionsID;
            parameters[2].Value = model.Answer;
            parameters[3].Value = model.ExamScore;
            parameters[4].Value = model.Creator;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.Editor;
            parameters[7].Value = model.EditTime;
            parameters[8].Value = model.DispOrder;
            parameters[9].Value = model.ValidMark;
            parameters[10].Value = model.Spare1;
            parameters[11].Value = model.Spare2;
            parameters[12].Value = model.Spare3;
            parameters[13].Value = model.Spare4;
            parameters[14].Value = model.SpareDecimal5;
            parameters[15].Value = model.SpareDecimal6;
            parameters[16].Value = model.SpareDecimal7;
            parameters[17].Value = model.SpareDecimal8;
            parameters[18].Value = model.ExamResultID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据

        /// </summary>
        public bool Delete(int ExamResultID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ExamResult ");
            strSql.Append(" where ExamResultID=@ExamResultID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExamResultID", SqlDbType.Int,4)
};
            parameters[0].Value = ExamResultID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据

        /// </summary>
        public bool DeleteList(string ExamResultIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ExamResult ");
            strSql.Append(" where ExamResultID in (" + ExamResultIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体

        /// </summary>
        public PS.Model.ExamResult GetModel(int ExamResultID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ExamResultID,ExamRecordID,ExamQuestionsID,Answer,ExamScore,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4,SpareDecimal5,SpareDecimal6,SpareDecimal7,SpareDecimal8 from ExamResult ");
            strSql.Append(" where ExamResultID=@ExamResultID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExamResultID", SqlDbType.Int,4)
};
            parameters[0].Value = ExamResultID;

            PS.Model.ExamResult model = new PS.Model.ExamResult();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ExamResultID"].ToString() != "")
                {
                    model.ExamResultID = int.Parse(ds.Tables[0].Rows[0]["ExamResultID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ExamRecordID"].ToString() != "")
                {
                    model.ExamRecordID = int.Parse(ds.Tables[0].Rows[0]["ExamRecordID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ExamQuestionsID"].ToString() != "")
                {
                    model.ExamQuestionsID = int.Parse(ds.Tables[0].Rows[0]["ExamQuestionsID"].ToString());
                }
                model.Answer = ds.Tables[0].Rows[0]["Answer"].ToString();
                if (ds.Tables[0].Rows[0]["ExamScore"].ToString() != "")
                {
                    model.ExamScore = decimal.Parse(ds.Tables[0].Rows[0]["ExamScore"].ToString());
                }
                model.Creator = ds.Tables[0].Rows[0]["Creator"].ToString();
                if (ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                model.Editor = ds.Tables[0].Rows[0]["Editor"].ToString();
                if (ds.Tables[0].Rows[0]["EditTime"].ToString() != "")
                {
                    model.EditTime = DateTime.Parse(ds.Tables[0].Rows[0]["EditTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DispOrder"].ToString() != "")
                {
                    model.DispOrder = int.Parse(ds.Tables[0].Rows[0]["DispOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ValidMark"].ToString() != "")
                {
                    model.ValidMark = int.Parse(ds.Tables[0].Rows[0]["ValidMark"].ToString());
                }
                model.Spare1 = ds.Tables[0].Rows[0]["Spare1"].ToString();
                model.Spare2 = ds.Tables[0].Rows[0]["Spare2"].ToString();
                model.Spare3 = ds.Tables[0].Rows[0]["Spare3"].ToString();
                model.Spare4 = ds.Tables[0].Rows[0]["Spare4"].ToString();
                if (ds.Tables[0].Rows[0]["SpareDecimal5"].ToString() != "")
                {
                    model.SpareDecimal5 = decimal.Parse(ds.Tables[0].Rows[0]["SpareDecimal5"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SpareDecimal6"].ToString() != "")
                {
                    model.SpareDecimal6 = decimal.Parse(ds.Tables[0].Rows[0]["SpareDecimal6"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SpareDecimal7"].ToString() != "")
                {
                    model.SpareDecimal7 = decimal.Parse(ds.Tables[0].Rows[0]["SpareDecimal7"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SpareDecimal8"].ToString() != "")
                {
                    model.SpareDecimal8 = decimal.Parse(ds.Tables[0].Rows[0]["SpareDecimal8"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ExamResultID,ExamRecordID,ExamQuestionsID,Answer,ExamScore,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4,SpareDecimal5,SpareDecimal6,SpareDecimal7,SpareDecimal8 ");
            strSql.Append(" FROM ExamResult ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据

        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ExamResultID,ExamRecordID,ExamQuestionsID,Answer,ExamScore,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark,Spare1,Spare2,Spare3,Spare4,SpareDecimal5,SpareDecimal6,SpareDecimal7,SpareDecimal8 ");
            strSql.Append(" FROM ExamResult ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "ExamResult";
            parameters[1].Value = "ExamResultID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetExamQuestions(int ExamRecordID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT  ExamResult.*, ExamQuestions.ExamContent, ExamQuestions.AnswerCount, ExamQuestions.CorrectAnswer, ExamQuestions.ExamType,ExamQuestionType.SingleQuestionScore ");
            strSql.Append("  FROM ExamResult INNER JOIN ExamQuestions ON ExamResult.ExamQuestionsID = ExamQuestions.ExamQuestionsID left OUTER JOIN ExamRecord ON ExamRecord.ExamRecordID =  ExamResult.ExamRecordID ");
            strSql.Append("  left outer JOIN ExamQuestionType  ON ExamQuestionType.ExamSysParaID =  ExamRecord.ExamSysParaID  AND ExamQuestionType.QuestionType=ExamQuestions.Examtype WHERE ExamResult.ExamRecordID='" + ExamRecordID + "'   Order by ExamQuestionType.DispOrder");

            return DbHelperSQL.Query(strSql.ToString());
        }


        #endregion  Method



    }
}

